home *** CD-ROM | disk | FTP | other *** search
/ The Games Machine 131 / XENIATGM131.iso / Shareware / openOffice.org 641 / Linux / f_0181 / Listbox.xba < prev    next >
Extensible Markup Language  |  2001-11-19  |  10KB  |  353 lines

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE script:module PUBLIC "-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "module.dtd">
  3. <script:module xmlns:script="http://openoffice.org/2000/script" script:name="Listbox" script:language="StarBasic">Option Explicit
  4. Dim OriginalList()
  5. Dim oDialogModel as Object
  6.  
  7.  
  8. Sub MergeList(SourceListBox() as Object, SecondList() as String)
  9. Dim i as Integer
  10. Dim MaxIndex as Integer
  11.     MaxIndex = Ubound(SecondList())
  12.     OriginalList() = AddListToList(OriginalList(), SecondList())
  13.     For i = 0 To MaxIndex
  14.         SourceListbox = AddSingleItemToListbox(SourceListbox, SecondList(i))
  15.     Next i
  16.     Call FormSetMoveRights()    
  17. End Sub
  18.  
  19.  
  20. Sub RemoveListItems(SourceListbox as Object, TargetListbox as Object, RemoveList() as String)
  21. Dim i as Integer
  22. Dim s as Integer
  23. Dim MaxIndex as Integer
  24. Dim CopyList()
  25.     MaxIndex = Ubound(RemoveList())
  26.     For i = 0 To MaxIndex
  27.         RemoveListboxItemByName(SourceListbox, RemoveList(i))
  28.         RemoveListboxItemByName(TargetListbox, RemoveList(i))
  29.     Next i
  30.     CopyList() = OriginalList()
  31.     s = 0 
  32.     MaxIndex = Ubound(CopyList())
  33.     For i = 0 To MaxIndex
  34.         If IndexInArray(CopyList(i),RemoveList())= -1 Then
  35.             OriginalList(s) = CopyList(i)
  36.             s = s + 1
  37.         End If
  38.     Next i
  39.     ReDim Preserve OriginalList(s-1)
  40.     Call FormSetMoveRights()    
  41. End Sub
  42.  
  43.  
  44. ' Note Boolean Parameter
  45. Sub InitializeListboxProcedures(oModel as Object, SourceListbox as Object, TargetListbox as Object)
  46. Dim EmptyList()
  47.     Set oDialogModel = oModel
  48.     OriginalList()= SourceListbox.StringItemList()
  49.     TargetListbox.StringItemList() = EmptyList()
  50. End Sub
  51.  
  52.  
  53. Sub CopyListboxItems(SourceListbox as Object, TargetListbox As Object)
  54. Dim NullArray()
  55.     TargetListbox.StringItemList() = OriginalList()
  56.     SourceListbox.StringItemList() = NullArray()
  57. End Sub
  58.  
  59.  
  60. Sub FormMoveSelected()
  61.     Call MoveSelectedListBox(oDialogModel.lstFields, oDialogModel.lstSelFields)
  62.     Call FormSetMoveRights()
  63.     oDialogModel.lstSelFields.Tag = True
  64. End Sub
  65.  
  66.  
  67. Sub FormMoveAll()
  68.     Call CopyListboxItems(oDialogModel.lstFields, oDialogModel.lstSelFields)
  69.     Call FormSetMoveRights()
  70.     oDialogModel.lstSelFields.Tag = True
  71. End Sub
  72.  
  73.  
  74. Sub FormRemoveSelected()
  75.     Call MoveOrderedSelectedListbox(oDialogModel.lstFields, oDialogModel.lstSelFields, False)
  76.     Call FormSetMoveRights()
  77.     oDialogModel.lstSelFields.Tag = True
  78. End Sub
  79.  
  80.  
  81. Sub FormRemoveAll()
  82.     Call MoveOrderedSelectedListbox(oDialogModel.lstFields, oDialogModel.lstSelFields, True)
  83.     Call FormSetMoveRights()
  84.     oDialogModel.lstSelFields.Tag = 1
  85. End Sub
  86.  
  87.  
  88. Sub MoveSelectedListBox(SourceListbox as Object, TargetListbox as Object)
  89. Dim MaxCurTarget as Integer
  90. Dim MaxSourceSelected as Integer
  91. Dim n as Integer
  92. Dim m as Integer
  93. Dim CurIndex
  94. Dim iOldTargetSelect as Integer
  95. Dim iOldSourceSelect as Integer
  96.     MaxCurTarget = Ubound(TargetListbox.StringItemList())
  97.     MaxSourceSelected = Ubound(SourceListbox.SelectedItems())    
  98.     Dim TargetList(MaxCurTarget+MaxSourceSelected+1) 
  99.     If MaxSourceSelected > -1 Then
  100.         iOldSourceSelect = SourceListbox.SelectedItems(0)
  101.         If Ubound(TargetListbox.SelectedItems()) > -1 Then
  102.             iOldTargetSelect = TargetListbox.SelectedItems(0)
  103.         Else 
  104.             iOldTargetSelect = -1
  105.         End If
  106.         For n = 0 To MaxCurTarget
  107.             TargetList(n) = TargetListbox.StringItemList(n)
  108.         Next n
  109.         For m = 0 To MaxSourceSelected
  110.             CurIndex = SourceListbox.SelectedItems(m)
  111.             TargetList(n) = SourceListbox.StringItemList(CurIndex)
  112.             n = n + 1
  113.         Next m
  114.         TargetListBox.StringItemList() = TargetList()
  115.         SourceListbox.StringItemList() = RemoveSelected (SourceListbox)
  116.         SetNewSelection(SourceListbox, iOldSourceSelect)
  117.         SetNewSelection(TargetListbox, iOldTargetSelect)
  118.     End If
  119. End Sub
  120.  
  121.  
  122.  
  123. Sub MoveOrderedSelectedListbox(lstSource as Object, lstTarget as Object, bMoveAll as Boolean)
  124. Dim NullArray()
  125. Dim MaxSelected as Integer
  126. Dim MaxSourceIndex as Integer
  127. Dim MaxOriginalIndex as Integer
  128. Dim MaxNewIndex as Integer
  129. Dim n as Integer
  130. Dim m as Integer
  131. Dim CurIndex as Integer
  132. Dim SearchString as String
  133. Dim SourceList() as String
  134. Dim iOldTargetSelect as Integer
  135. Dim iOldSourceSelect as Integer
  136.     If bMoveAll Then
  137.         lstSource.StringItemList() = OriginalList()
  138.         lstTarget.StringItemList() = NullArray()
  139.     Else
  140.         MaxOriginalIndex = Ubound(OriginalList())
  141.         MaxSelected = Ubound(lstTarget.SelectedItems())
  142.         iOldTargetSelect = lstTarget.SelectedItems(0)
  143.         If Ubound(lstSource.SelectedItems()) > -1 Then
  144.             iOldSourceSelect = lstSource.SelectedItems(0)
  145.         End If
  146.         Dim SelList(MaxSelected)
  147.         For n = 0 To MaxSelected
  148.             CurIndex = lstTarget.SelectedItems(n)
  149.             SelList(n) = lstTarget.StringItemList(CurIndex)
  150.         Next n
  151.         SourceList() = lstSource.StringItemList()
  152.         MaxSourceIndex = Ubound(lstSource.StringItemList())
  153.         MaxNewIndex = MaxSelected + MaxSourceIndex + 1
  154.         Dim NewSourceList(MaxNewIndex)
  155.         m = 0
  156.         For n = 0 To MaxOriginalIndex
  157.             SearchString = OriginalList(n)
  158.             If IndexinArray(SearchString, SelList()) <> -1 Then
  159.                 NewSourceList(m) =  SearchString
  160.                 m = m + 1
  161.             ElseIf IndexinArray(SearchString, SourceList()) <> -1 Then
  162.                 NewSourceList(m) =  SearchString
  163.                 m = m + 1
  164.             End If
  165.         Next n
  166.         lstSource.StringItemList() = NewSourceList()
  167.         lstTarget.StringItemList() = RemoveSelected(lstTarget)
  168.     End If
  169. ' Todo: Hier weitermachen:
  170.     SetNewSelection(lstSource, iOldSourceSelect)
  171.     SetNewSelection(lstTarget, iOldTargetSelect)
  172.  
  173. End Sub
  174.  
  175.  
  176. Function RemoveSelected(oListbox as Object)
  177. Dim MaxIndex as Integer
  178. Dim MaxSelected as Integer
  179. Dim n as Integer
  180. Dim m as Integer
  181. Dim CurIndex as Integer
  182. Dim CurItem as String
  183. Dim ResultArray()
  184.     MaxIndex = Ubound(oListbox.StringItemList())
  185.     MaxSelected = Ubound(oListbox.SelectedItems())
  186.     Dim LocItemList(MaxIndex)
  187.     LocItemList() = oListbox.StringItemList()
  188.     If MaxSelected > -1 Then
  189.         For n = 0 To MaxSelected
  190.             CurIndex = oListbox.SelectedItems(n)
  191.             LocItemList(CurIndex) = ""
  192.         Next n
  193.         If MaxIndex > 0 Then
  194.             ReDim ResultArray(MaxIndex - MaxSelected - 1)
  195.             m = 0
  196.             For n = 0 To MaxIndex
  197.                 CurItem = LocItemList(n)
  198.                 If CurItem <> "" Then
  199.                     ResultArray(m) = CurItem
  200.                     m = m + 1
  201.                 End If
  202.             Next n    
  203.         End If
  204.         RemoveSelected = ResultArray()    
  205.     Else
  206.         RemoveSelected = oListbox.StringItemList()    
  207.     End If
  208. End Function
  209.  
  210.  
  211. Sub SetNewSelection(oListBox as Object, iLastSelection as Integer)
  212. Dim MaxIndex as Integer
  213. Dim SelIndex as Integer
  214. Dim SelList(0) as Integer
  215.     MaxIndex = Ubound(oListBox.StringItemList())
  216.     If MaxIndex > -1  AND iLastSelection > -1 Then
  217.         If iLastSelection > MaxIndex Then
  218.             Selindex = MaxIndex
  219.         Else
  220.             SelIndex = iLastSelection
  221.         End If
  222.         Sellist(0) = SelIndex
  223.         oListBox.SelectedItems() = SelList()
  224.     End If
  225. End Sub
  226.  
  227.  
  228. Sub ToggleListboxControls(oDialogModel as Object, bDoEnable as Boolean)
  229.     With oDialogModel
  230.         .lblFields.Enabled = bDoEnable
  231.         .lblSelFields.Enabled = bDoEnable
  232.         .lstTables.Enabled = bDoEnable
  233.         .lstFields.Enabled = bDoEnable
  234.         .lstSelFields.Enabled = bDoEnable
  235.         .cmdRemoveAll.Enabled = bDoEnable
  236.         .cmdRemoveSelected.Enabled = bDoEnable
  237.         .cmdMoveAll.Enabled = bDoEnable
  238.         .cmdMoveSelected.Enabled = bDoEnable
  239.     End With
  240.     If bDoEnable Then
  241.         FormSetMoveRights()
  242.     End If
  243. End Sub    
  244.  
  245.  
  246. ' Enable or disable the buttons used for moving the available
  247. ' fields between the two list boxes.
  248. Sub FormSetMoveRights()
  249. Dim bIsFieldSelected as Boolean
  250. Dim bSelectSelected as Boolean
  251. Dim FieldCount as Integer
  252. Dim SelectCount as Integer
  253.     bIsFieldSelected = Ubound(oDialogModel.lstFields.SelectedItems()) <> -1
  254.     FieldCount = Ubound(oDialogModel.lstFields.StringItemList()) + 1
  255.     bSelectSelected = Ubound(oDialogModel.lstSelFields.SelectedItems()) > -1
  256.     SelectCount = Ubound(oDialogModel.lstSelFields.StringItemList()) + 1
  257.     oDialogModel.cmdRemoveAll.Enabled = SelectCount>=1
  258.     oDialogModel.cmdRemoveSelected.Enabled = bSelectSelected
  259.     oDialogModel.cmdMoveAll.Enabled = FieldCount >=1
  260.     oDialogModel.cmdMoveSelected.Enabled = bIsFieldSelected
  261.     oDialogModel.cmdGoOn.Enabled = SelectCount>=1
  262.     ' This flag is set to '1' when the lstSelFields has been modified 
  263. End Sub
  264.  
  265.  
  266. Function AddSingleItemToListbox(ByVal oListbox as Object, ListItem as String, Optional iSelIndex) as Object
  267. Dim MaxIndex as Integer
  268. Dim i as Integer
  269.  
  270.     MaxIndex = Ubound(oListbox.StringItemList())
  271. Dim LocList(MaxIndex + 1)
  272. ' Todo: This goes faster with the Redim LocList(MaxIndex + 1) Preserve function
  273.     For i = 0 To MaxIndex
  274.         LocList(i) = oListbox.StringItemList(i)
  275.     Next i
  276.     LocList(MaxIndex + 1) = ListItem
  277.     oListbox.StringItemList() = LocList()
  278.     If Not IsMissing(iSelIndex) Then
  279.         SelectListboxItem(oListbox, iSelIndex)
  280.     End If
  281.     AddSingleItemToListbox() = oListbox
  282. End Function
  283.  
  284.  
  285. Sub    EmptyListbox(oListbox as Object)
  286. Dim NullList() as String
  287.     oListbox.StringItemList() = NullList()
  288. End Sub
  289.  
  290.  
  291. Sub    SelectListboxItem(oListbox as Object, iSelIndex as Integer)
  292. Dim LocSelList(0) as Integer
  293.     If iSelIndex <> -1 Then
  294.         LocSelList(0) = iSelIndex
  295.         oListbox.SelectedItems() = LocSelList()
  296.     End If
  297. End Sub
  298.  
  299.  
  300. Function GetSelectedListboxItems(oListbox as Object)
  301. Dim SelList() as String
  302. Dim i as Integer
  303. Dim CurIndex as Integer
  304.     For i = 0 To Ubound(oListbox.SelectedItems())
  305.         CurIndex = oListbox.SelectedItems(i)
  306.         SelList(i) = oListbox.StringItemList(CurIndex)
  307.     Next i
  308.     GetSelectedListboxItems() = SelList()
  309. End Function
  310.  
  311.  
  312. ' Note: When using this Sub it must be ensured that the
  313. ' 'RemoveItem' appears only only once in the Listbox
  314. Sub RemoveListboxItemByName(oListbox as Object, RemoveItem as String)
  315. Dim OldList() as String
  316. Dim NullList() as String
  317. Dim i as Integer
  318. Dim a as Integer
  319. Dim MaxIndex as Integer
  320.     OldList = oListbox.StringItemList()
  321.     MaxIndex = Ubound(OldList())
  322.     If IndexInArray(RemoveItem, OldList()) <> -1 Then
  323.         If MaxIndex > 0 Then
  324.             a = 0
  325.             Dim NewList(MaxIndex -1)
  326.             For i = 0 To MaxIndex
  327.                 If RemoveItem <> OldList(i) Then
  328.                     NewList(a) = OldList(i)
  329.                     a = a + 1
  330.                 End If
  331.             Next i
  332.             oListbox.StringItemList() = NewList()
  333.         Else
  334.             oListBox.StringItemList() = NullList()
  335.         End If
  336.     End If
  337. End Sub
  338.  
  339.  
  340. Function GetItemPos(oListBox as Object, sItem as String)
  341. Dim ItemList()
  342. Dim MaxIndex as Integer
  343. Dim i as Integer
  344.     ItemList() = oListBox.StringItemList()
  345.     MaxIndex = Ubound(ItemList())    
  346.     For i = 0 To MaxIndex
  347.         If sItem = ItemList(i) Then
  348.             GetItemPos() = i
  349.             Exit Function
  350.         End If
  351.     Next i
  352.     GetItemPos() = -1
  353. End Function</script:module>